 package gui;
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
//import java.awt.GridBagConstraints;
//import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
//import java.io.BufferedReader;
//import java.io.BufferedWriter;
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.FileNotFoundException;
//import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
//import java.io.InputStreamReader;
//import java.io.OutputStreamWriter;
//import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDesktopPane;
//import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
//import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
//import javax.swing.JOptionPane;
import javax.swing.JEditorPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
//import javax.swing.border.CompoundBorder;
//import javax.swing.border.EmptyBorder;
//import javax.swing.border.TitledBorder;
//import javax.swing.filechooser.FileNameExtensionFilter;
//import javax.xml.parsers.DocumentBuilder;
//import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.jvnet.substance.SubstanceLookAndFeel;
//import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import Lexicon.Lexicon;
//import Lexicon.WordType;

public class ArMT extends JDesktopPane implements ActionListener {
	/**
	 * 
	 */
	public static String FILE_SEPARATOR=System.getProperty("file.separator");
	private static final long serialVersionUID = 1L;
	public static JFrame frame ;
	private static ArMT gui ;
	ArMT armt ;
//	JFileChooser dicChooser = new JFileChooser();
//	JFileChooser txtChooser =new JFileChooser();
//	JFileChooser saveDic = new JFileChooser();
//	JFileChooser saveTxt = new JFileChooser();
//	//private JPanel statusBar;
//	String [] xmlFilter = {"xml"};
//	String [] txtFilter = {"txt","doc"};
//	FileNameExtensionFilter xmlExten = new FileNameExtensionFilter ( "XML Files" , xmlFilter) ;
//	FileNameExtensionFilter txtExten = new FileNameExtensionFilter ( "Text Files" ,txtFilter) ;
	TranslationFrame transFrame ;
//	private File currentDirectory;
//    private File sourceFile;
//    private File targetFile;
    //Internationalisation and localisation objects    
    public static ResourceBundle labels;
    private Locale currentLocale;
    static ComponentOrientation co;
    JPanel encPanel ;
//    private String[] encOptions = {"Cp420", "Cp1256", "ISO8859_6", "MacArabic", "UTF8", "UTF-16"};
    String encoding = "Cp1256"; 
    JComboBox encodingOptions ;
    Lexicon lex ;
//    JArabicKB arKB;
   // private int propDefaultWindowSize;
    
    public ArMT() throws UnsupportedLookAndFeelException
	{
    	super(); 
        loadResources();
        co = ComponentOrientation.getOrientation(currentLocale);
      //  currentDirectory = new File("resources");
        try {
			lex = new Lexicon() ;
			createFrame() ;
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
    protected void addButtons(JToolBar toolBar) {
        JButton newDocButton = new JButton() ;
        newDocButton.setIcon(new ImageIcon("resources"+FILE_SEPARATOR+"images"+FILE_SEPARATOR+"new_doc.gif"));
        newDocButton.setActionCommand("new");     
        newDocButton.addActionListener(this);
        toolBar.add(newDocButton);
        
        JButton openDocButton = new JButton() ;
        openDocButton.setIcon(new ImageIcon("resources"+FILE_SEPARATOR+"images"+FILE_SEPARATOR+"open_doc.gif"));
        openDocButton.setActionCommand("import_txt"); 
        openDocButton.addActionListener(this);
        toolBar.add(openDocButton);
    }
    public static  void applyComponentOrientation(Component c, ComponentOrientation o) {
        c.setComponentOrientation(o);
        if (c instanceof JMenu) {
            JMenu menu = (JMenu) c;
            int nComponents = menu.getMenuComponentCount();
            for (int i = 0; i < nComponents; ++i) {
                applyComponentOrientation(menu.getMenuComponent(i), o);
            }
        }
        else if (c instanceof Container) {
            Container container = (Container) c;
            int nComponents = container.getComponentCount();
            for (int i = 0; i < nComponents; ++i) {
                applyComponentOrientation(container.getComponent(i), o);
            }
        }
    }
    private void loadResources() {
        try {

            // Language resources...
            currentLocale = Locale.getDefault();
            labels = ResourceBundle.getBundle("resources.LabelsBundle", currentLocale,this.getClass().getClassLoader());
        }
        catch (MissingResourceException mre) {
            mre.printStackTrace(System.err);
            System.exit(1);
        }
    }
    public JToolBar createToolBar()
    {
    	JToolBar toolBar = new JToolBar();
    	if (co == ComponentOrientation.RIGHT_TO_LEFT)
    		toolBar.setLayout(new FlowLayout(FlowLayout.RIGHT,10,3));
    	else toolBar.setLayout(new FlowLayout(FlowLayout.LEFT,10,3)); 
        JButton newDocButton = new JButton() ;
        newDocButton.setIcon(new ImageIcon("resources"+FILE_SEPARATOR+"images"+FILE_SEPARATOR+"new_doc.gif"));
        newDocButton.setActionCommand("new");     
        newDocButton.addActionListener(this);
        toolBar.add(newDocButton);      
        
        return toolBar ;
    }
    public JMenuBar createMenuBar() {
        JMenuBar menuBar = new JMenuBar();

        //Set up the File menu.
        {
        JMenu fileMenu = new JMenu(labels.getString("fileMenu"));
        fileMenu.setMnemonic(KeyEvent.VK_F);
        fileMenu.setFont(new Font("Lucida Sans", Font.BOLD, 12));
        menuBar.add(fileMenu);

        //Set up the new menu item.
        JMenuItem newMenuItem = new JMenuItem(labels.getString("fileNewMenue"));
        newMenuItem.setMnemonic(KeyEvent.VK_N);
        newMenuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_N, ActionEvent.ALT_MASK));
        newMenuItem.setActionCommand("new");
        newMenuItem.addActionListener(this);
        newMenuItem.setFont(new Font("Lucida Sans", Font.BOLD, 12));
        fileMenu.add(newMenuItem);
        
        
//        //   set up Import menu 
//        JMenu importMenu = new JMenu(labels.getString("fileImportMenu"));
//        importMenu.setMnemonic(KeyEvent.VK_M);
//       // fileMenu.add(importMenu);
//        {
//        JMenuItem importDictMnuItem = new JMenuItem(labels.getString("fileImportDicitonaryMenueItem"));
//        importDictMnuItem.setMnemonic(KeyEvent.VK_D);
//        importDictMnuItem.setAccelerator(KeyStroke.getKeyStroke(
//                KeyEvent.VK_D, ActionEvent.ALT_MASK));
//        importDictMnuItem.setActionCommand("import_dict");
//        importDictMnuItem.addActionListener(this);
//        importDictMnuItem.setFont(new Font("Lucida Sans", Font.BOLD, 12));
//        importMenu.add(importDictMnuItem);
//        
//        JMenuItem importTxtMnuItem = new JMenuItem(labels.getString("fileImportTextMenuItem"));
//        importTxtMnuItem.setMnemonic(KeyEvent.VK_T);
//        importTxtMnuItem.setAccelerator(KeyStroke.getKeyStroke(
//                KeyEvent.VK_T, ActionEvent.ALT_MASK));
//        importTxtMnuItem.setActionCommand("import_txt");
//        importTxtMnuItem.setFont(new Font("Lucida Sans", Font.BOLD, 12));
//        importTxtMnuItem.addActionListener(this);
//        importMenu.add(importTxtMnuItem);
//        }
//      //set up Export menu 
//        JMenu exporetMenu = new JMenu(labels.getString("fileExporetMenu"));
//        exporetMenu.setMnemonic(KeyEvent.VK_X);
//        //fileMenu.add(exporetMenu);
//         // Set up the exp .dict menu item.
//        {  JMenuItem exportDictMnuItem = new JMenuItem(labels.getString("fileExportDictionaryMenuItem"));
//        exportDictMnuItem.setMnemonic(KeyEvent.VK_I);
//        exportDictMnuItem.setAccelerator(KeyStroke.getKeyStroke(
//                KeyEvent.VK_I, ActionEvent.ALT_MASK));
//        exportDictMnuItem.setFont(new Font("Lucida Sans", Font.BOLD, 12));
//        exportDictMnuItem.setActionCommand("export_dict");
//        exportDictMnuItem.addActionListener(this);
//        exporetMenu.add(exportDictMnuItem);
//        
//        JMenuItem exportTxtMnuItem = new JMenuItem(labels.getString("fileExportTextMenuItem"));
//        exportTxtMnuItem.setMnemonic(KeyEvent.VK_E);
//        exportTxtMnuItem.setAccelerator(KeyStroke.getKeyStroke(
//                KeyEvent.VK_E, ActionEvent.ALT_MASK));
//        exportTxtMnuItem.setActionCommand("export_txt");
//        exportTxtMnuItem.addActionListener(this);
//        exportTxtMnuItem.setFont(new Font("Lucida Sans", Font.BOLD, 12));
//        exporetMenu.add(exportTxtMnuItem);     
//        }
//        JMenuItem printMenuItem = new JMenuItem(labels.getString("filePrintMenuItem"));
//        printMenuItem.setMnemonic(KeyEvent.VK_P);
//        printMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.ALT_MASK));
//        printMenuItem.setActionCommand("print");
//        printMenuItem.setFont(new Font("Lucida Sans", Font.BOLD, 12));
//        printMenuItem.addActionListener(this);
//        //fileMenu.add(printMenuItem);

        //Set up the quit menu item.
        JMenuItem quitMenuItem = new JMenuItem(labels.getString("fileMenuExitItem"));
        quitMenuItem.setMnemonic(KeyEvent.VK_Q);
        quitMenuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_Q, ActionEvent.ALT_MASK));
        quitMenuItem.setActionCommand("quit");
        quitMenuItem.setFont(new Font("Lucida Sans", Font.BOLD, 12));
        quitMenuItem.addActionListener(this);
        fileMenu.add(quitMenuItem);
        }
       //  Set up the setting menu.
        {
        	JMenu settingMenu = new JMenu(labels.getString("settingsMenu"));
        	settingMenu.setMnemonic(KeyEvent.VK_S);
        	settingMenu.setFont(new Font("Lucida Sans", Font.BOLD, 10));
            menuBar.add(settingMenu);
            
            
            //   create the language submenu

            JMenu langMenu = new LocaleChanger();
            
            settingMenu.add(langMenu);    
        }
        //      Set up the tools menu.
        {
        	JMenu toolsMenu = new JMenu(labels.getString("toolsMenu"));
        	toolsMenu.setMnemonic(KeyEvent.VK_O);
        	toolsMenu.setFont(new Font("Lucida Sans", Font.BOLD, 12));
            menuBar.add(toolsMenu);
            
            
            //   create the arKB submenu  toolsMenuArKB

            JMenuItem arKBMenuItem = new JMenuItem(labels.getString("toolsMenuArKB"));
            arKBMenuItem.setMnemonic(KeyEvent.VK_A);
            arKBMenuItem.setAccelerator(KeyStroke.getKeyStroke(
                    KeyEvent.VK_A, ActionEvent.ALT_MASK));
            arKBMenuItem.setActionCommand("ar_kb");
            arKBMenuItem.setFont(new Font("Lucida Sans", Font.BOLD, 12));
            arKBMenuItem.addActionListener(this);
            toolsMenu.add(arKBMenuItem);     
            
            JMenuItem stemMenuItem = new JMenuItem("Stemmer");
            stemMenuItem.setMnemonic(KeyEvent.VK_T);
            stemMenuItem.setAccelerator(KeyStroke.getKeyStroke(
                    KeyEvent.VK_T, ActionEvent.ALT_MASK));
            stemMenuItem.setActionCommand("stem");
            stemMenuItem.setFont(new Font("Lucida Sans", Font.BOLD, 12));
            stemMenuItem.addActionListener(this);
            toolsMenu.add(stemMenuItem);  
            
            
        }
        

        //      Set up the Help menu.
        {
        	JMenu helpMenu = new JMenu(labels.getString("helpMenu"));
        	helpMenu.setMnemonic(KeyEvent.VK_H);
        	helpMenu.setFont(new Font("Lucida Sans", Font.BOLD, 12));
            menuBar.add(helpMenu);
            
            
            //   create the submenus

            JMenuItem helpConMenuItem = new JMenuItem(labels.getString("helpMenuContentsItem"));
            helpConMenuItem.setMnemonic(KeyEvent.VK_C);
            helpConMenuItem.setAccelerator(KeyStroke.getKeyStroke(
                    KeyEvent.VK_C, ActionEvent.ALT_MASK));
            helpConMenuItem.setActionCommand("help_con");
            helpConMenuItem.setFont(new Font("Lucida Sans", Font.BOLD, 12));
            helpConMenuItem.addActionListener(this); 
            helpMenu.add(helpConMenuItem); 
            
            JMenuItem helpAboutMenuItem = new JMenuItem(labels.getString("helpMenuAboutItem"));
            helpAboutMenuItem.setMnemonic(KeyEvent.VK_A);
            helpAboutMenuItem.setAccelerator(KeyStroke.getKeyStroke(
                    KeyEvent.VK_A, ActionEvent.ALT_MASK));
            helpAboutMenuItem.setActionCommand("help_about");
            helpAboutMenuItem.addActionListener(this); 
            helpMenu.add(helpAboutMenuItem);   
        }
        return menuBar;
    }
//	React to menu selections.
    public void actionPerformed(ActionEvent e) {
        if ("new".equals(e.getActionCommand())) { //new
            try {
				createFrame();
			} catch (SAXException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (ParserConfigurationException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
        }
        else if("lang".equals(e.getActionCommand())){//language
            
        }
        else if("ar_kb".equals(e.getActionCommand())){
        	JInternalFrame kbFrame = new JInternalFrame(ArMT.labels.getString("ArKB"), 
                    true, //resizable
                    true, //closable
                    false, //maximizable
                    true);
        	JArabicKB arKB = new JArabicKB();
    		kbFrame.add(arKB );
    		//frame.getContentPane().
//        	frame.getContentPane().add (new JArabicKB() ,BorderLayout.EAST);
    		applyComponentOrientation(kbFrame,co);
    		kbFrame.setTitle(ArMT.labels.getString("ArKB"));
    		kbFrame.pack();
    		kbFrame.setVisible(true);
    		add(kbFrame);
    		kbFrame.setLocation(/*TranslationFrame.getXLocation()+*/700 ,TranslationFrame.getYLocation());
        }
        else if("stem".equals(e.getActionCommand())){
        	JInternalFrame stemFrame = new JInternalFrame("Stemmer", 
                    true, //resizable
                    true, //closable
                    false, //maximizable
                    true);
        	JTextField stemWord = new JTextField();
        	add(stemFrame );
    		//frame.getContentPane().
//        	frame.getContentPane().add (new JArabicKB() ,BorderLayout.EAST);
    		applyComponentOrientation(stemWord,ComponentOrientation.RIGHT_TO_LEFT);
    		stemFrame.setTitle("Stemmer");
    		stemFrame.pack();
    		stemFrame.setVisible(true);
    		add(stemFrame);
    		stemFrame.setLocation(/*TranslationFrame.getXLocation()+*/600 ,TranslationFrame.getYLocation()+20);
        }
        else if("help_con".equals(e.getActionCommand())){
        	JFrame helpConFrame = new JFrame("Help contenents");
        	try {
        		
				helpConFrame.add(new JScrollPane(new HelpConPane()));
			} catch (MalformedURLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
    		//frame.getContentPane().
//        	frame.getContentPane().add (new JArabicKB() ,BorderLayout.EAST);
    		applyComponentOrientation(helpConFrame,co);
    		//helpConFrame.setTitle(ArMT.labels.getString("helpConFrame"));
    		helpConFrame.pack();
    		helpConFrame.setVisible(true);
    		helpConFrame.setLocation(700,TranslationFrame.getYLocation());
        }
        else if("help_about".equals(e.getActionCommand())){
        	String message = ArMT.labels.getString("programTitle");
            message += "\nVersion : This program is under construction \n\n";
            message += "\nDevelopers \n\t\t" + "Hind Abdulkhaleq";
            message += "\nSupervisor by\n\t"+"\tDr.Samir AlMougy\n";
            message += "\n\tComputer Science departement" ;
            message += "\n\tFaculty of Computer Science and Information Systems";
            message += "\n\tMansoura University \n";
            message += "\n\t\tEgypt\n\n";
            JOptionPane.showMessageDialog(
                    this, 
                    message,
                    labels.getString("helpMenuAboutItem"),
                    JOptionPane.PLAIN_MESSAGE);   
        }
//        else if ("import_dict".equals(e.getActionCommand())){
//        	 OpenDictDialog();
//        }
//        else if ("import_txt".equals(e.getActionCommand())){
//        	try {
//				OpenTxtDialog();
//			} catch (IOException e1) {
//				// TODO Auto-generated catch block
//				e1.printStackTrace();
//			}
//        }
//        else if ("export_dict".equals(e.getActionCommand())){
//       	   try {
//			saveDictDialog();
//		} catch (FileNotFoundException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		} catch (UnsupportedEncodingException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}
//       }
//        else if ("export_txt".equals(e.getActionCommand())){
//       	   try {
//			SaveTxtDialog();
//		} catch (FileNotFoundException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		} catch (UnsupportedEncodingException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		} catch (IOException e2) {
//			// TODO Auto-generated catch block
//			e2.printStackTrace();
//		}
//       }
        else if("print".equals(e.getActionCommand()))
        {
        	FileWriter out;
			try {
				out = new FileWriter("lpt1");
				 out.write(transFrame.getOutputText());
		            out.write(0x0D); // CR
		            out.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
           
        }
        else if ("help_con".equals(e.getActionCommand())){
          	 
          }
        else if ("help_con".equals(e.getActionCommand())){
          	 
          }
        else { //quit
            quit();
        }
    }
//    private void SaveTxtDialog() throws IOException {	
//		saveTxt.addChoosableFileFilter(txtExten);
//    	saveTxt.setCurrentDirectory(currentDirectory);
//    	saveTxt.setDialogTitle(ArMT.labels.getString("saveTxtDialog"));
//    	addEncodingAccessory(saveTxt);
//    	int returnVal = saveTxt.showSaveDialog(this);
//        if (returnVal == JFileChooser.APPROVE_OPTION ) {
//        	targetFile = saveTxt.getSelectedFile();   
//        	if (targetFile==null||targetFile.getName().equals (" "))
//    		{
//    			JOptionPane.showMessageDialog( null,labels.getString("InvalidFileName"),labels.getString("InvalidFileName"),JOptionPane.ERROR_MESSAGE );
//    		}
//        	if ((String)encodingOptions.getSelectedItem() != null) {
//                encoding =(String)encodingOptions.getSelectedItem();
//            }
//        	FileOutputStream file = new FileOutputStream(targetFile);
//    		OutputStreamWriter o = new OutputStreamWriter(file,encoding);
//    		BufferedWriter bw = new BufferedWriter(o);      
//			transFrame.getOutputTextArea().write(bw);
//			bw.flush();
//			bw.close();
//            }
//        else if(returnVal == JFileChooser.CANCEL_OPTION)
//        {
//        	return ;
//        }
//        currentDirectory = sourceFile;
//
//	}
//	private void saveDictDialog() throws FileNotFoundException, UnsupportedEncodingException {
//		saveDic.addChoosableFileFilter(xmlExten);
//		saveDic.setDialogTitle(ArMT.labels.getString("saveDicDialog"));
//		saveDic.setCurrentDirectory(currentDirectory);
//		addEncodingAccessory(saveDic);
//		int returnVal = saveDic.showSaveDialog(this);
//        if (returnVal == JFileChooser.APPROVE_OPTION ) {
//        	targetFile = saveDic.getSelectedFile();   
//        	if (targetFile==null||targetFile.getName().equals (" "))
//    		{
//    			JOptionPane.showMessageDialog( null,labels.getString("InvalidFileName"),labels.getString("InvalidFileName"),JOptionPane.ERROR_MESSAGE );
//    		}
//        	if ((String)encodingOptions.getSelectedItem() != null) {
//                encoding =(String)encodingOptions.getSelectedItem();
//            }
//        try {
//			lex.store_data(targetFile.getAbsolutePath(),encoding );
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//        }
//	}
//	private void OpenDictDialog() {				
//		dicChooser.addChoosableFileFilter(xmlExten);
//		dicChooser.setCurrentDirectory(currentDirectory);
//		dicChooser.setDialogTitle(ArMT.labels.getString("openDicDialog"));
//		dicChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
//		addEncodingAccessory(dicChooser);
//		int returnVal = dicChooser.showOpenDialog(this);
//        if (returnVal == JFileChooser.APPROVE_OPTION) {
//        	sourceFile = dicChooser.getSelectedFile();
//            try {
//				DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
//				DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
//				Document doc = docBuilder.parse(new File(sourceFile.getAbsolutePath()));
//		         doc.getDocumentElement().normalize ();//TODO switch for data type ,defined nouns ,indeter nouns ,verbs
//		        try{
//		         lex.load_4_Data(doc.getElementsByTagName("entry"),WordType.UN_KNOWN);
//		        }catch(NullPointerException ne)
//		        {
//		        	lex.load_3_Data(doc.getElementsByTagName("entry"));
//		        }
//			} 
//            catch (SAXException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} catch (ParserConfigurationException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}        
//				currentDirectory = sourceFile;
//            }    
//        
//	}
//	public void OpenTxtDialog() throws IOException {
//		txtChooser.addChoosableFileFilter(txtExten);
//		txtChooser.setCurrentDirectory(currentDirectory);
//		txtChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
//		txtChooser.setDialogTitle(ArMT.labels.getString("openTxtDialog"));
//		addEncodingAccessory(txtChooser);
//		if(transFrame.isVisible() == false)
//		{
//			try {
//				createFrame();
//			} catch (SAXException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} catch (ParserConfigurationException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
//		int returnVal = txtChooser.showOpenDialog(this);
//        if (returnVal == JFileChooser.APPROVE_OPTION ) {
//        	sourceFile = txtChooser.getSelectedFile();   
//        	if (sourceFile==null||sourceFile.getName().equals (" "))
//    		{
//    			JOptionPane.showMessageDialog( null,labels.getString("InvalidFileName"),labels.getString("InvalidFileName"),JOptionPane.ERROR_MESSAGE );
//    		}
//        	if ((String)encodingOptions.getSelectedItem() != null) {
//                encoding =(String)encodingOptions.getSelectedItem();
//            }
//        	FileInputStream file = new FileInputStream(sourceFile);
//    		InputStreamReader i = new InputStreamReader(file,encoding);
//    		BufferedReader br = new BufferedReader(i);
//            //BufferedReader br = new BufferedReader(new FileReader(sourceFile));
//    		currentDirectory = sourceFile;
////    		String buf ,s = null ;
////			while( (buf = br.readLine()) != null )
////			{
////				s += buf;
////				s +="\n";
////				transFrame.setInpuText(s);
////			}
//			TranslationFrame.getInputTextArea().read(br, null);
//			br.close();
//            }
//        else if(returnVal == JFileChooser.CANCEL_OPTION)
//        {
//        	return ;
//        }
//        currentDirectory = sourceFile;
//}
//	private void addEncodingAccessory(JFileChooser fc) {
//
//        GridBagLayout gridbag = new GridBagLayout();
//        GridBagConstraints c = new GridBagConstraints();
//        c.fill = GridBagConstraints.BOTH;
//        c.weightx = 1.0;
//        c.ipadx = 2;
//        c.ipady = 2;
//
//        encPanel = new JPanel(gridbag);
//
//        encPanel.setBorder(new CompoundBorder(
//                new CompoundBorder(
//                    new EmptyBorder(5, 5, 5, 5),
//                    new TitledBorder(labels.getString("saveDialogOptions"))),
//                new EmptyBorder(5, 5, 5, 5)));
//        JLabel saveEncLabel = new JLabel(labels.getString("saveDialogSaveEnc"));
//        
//        encodingOptions = new JComboBox(encOptions);
//        encodingOptions.setSelectedItem(encoding);
//
//        c.gridwidth = 1;
//        gridbag.setConstraints(saveEncLabel, c);
//        encPanel.add(saveEncLabel);
//        c.gridwidth = GridBagConstraints.REMAINDER;
//        gridbag.setConstraints(encodingOptions, c);
//        encPanel.add(encodingOptions);
//        fc.setAccessory(encPanel);
//
//    }

    //Create a new internal frame.
    protected void createFrame() throws SAXException, IOException, ParserConfigurationException {
    	transFrame = new TranslationFrame();
        super.add(transFrame);
        transFrame.setVisible(true); //necessary as of 1.3
        try {
        	transFrame.setSelected(true);
        } catch (java.beans.PropertyVetoException e) {}
    }

    //Quit the application.
    protected void quit() {
        System.exit(0);
    }
    class LocaleChanger extends JMenu implements ItemListener {
    /**
		 * 
		 */
		private static final long serialVersionUID = 1L;
	public LocaleChanger() {
    	super();
    	setText(labels.getString("settingsMenuLanguage"));
    	setMnemonic(KeyEvent.VK_L);

        ButtonGroup langGroup = new ButtonGroup();
       //String language = Locale.getDefault().getLanguage();

        // Sort the language names according to the rules specific to each locale
        //RuleBasedCollator rbc = (RuleBasedCollator)Collator.getInstance();
        ArrayList<String> al = new ArrayList<String>();
        al.add(labels.getString("settingsMenuLanguageArabicItem"));
        al.add(labels.getString("settingsMenuLanguageEnglishItem"));

        //Collections.sort(al, rbc);

        String langName = Locale.getDefault().getDisplayLanguage();
        for (int i = 0; i < al.size(); i++) {
          JRadioButtonMenuItem mi;
          mi = (JRadioButtonMenuItem)
          add(new JRadioButtonMenuItem((String)al.get(i)));
          //mi.setFont(menuFont);
          if (langName.equalsIgnoreCase((String)al.get(i)))
           mi.setSelected(true);
          mi.addItemListener(this);
          langGroup.add(mi);
          
        }
       
      }
      public void itemStateChanged(ItemEvent e) {
        try {      
        JRadioButtonMenuItem rb = (JRadioButtonMenuItem) e.getSource();
        if (rb.isSelected()) {
          String selected = rb.getText();
          if (selected.equals(labels.getString("settingsMenuLanguageArabicItem"))) {
        	  
        	  //if(Locale.getDefault() == Locale.UK)
          	{
            Locale.setDefault(new Locale("ar", "EG"));
            co = ComponentOrientation.RIGHT_TO_LEFT;
            
          	}
          }
    else if (selected.equals(labels.getString("settingsMenuLanguageEnglishItem"))) {
          		//if(Locale.getDefault() != Locale.UK)
              	{
            Locale.setDefault(new Locale("en","UK"));
            co = ComponentOrientation.LEFT_TO_RIGHT;            
              	}
        }
        if (co != null) {
        armt = new ArMT();
          ArMT.frame.getContentPane().removeAll();
          ArMT.frame.getContentPane().add(armt.createToolBar(),BorderLayout.PAGE_START);
          ArMT.frame.setJMenuBar(armt.createMenuBar());
          ArMT.frame.setLocale(currentLocale);
          ArMT.frame.setContentPane(armt);
          ArMT.frame.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
          ArMT.applyComponentOrientation(frame,co);
          ArMT.applyComponentOrientation(armt.createMenuBar(),co);
          ArMT.applyComponentOrientation(armt.createToolBar(),co);
          ArMT.frame.repaint();
        }
        }
        }catch (Exception ex) {
            ex.printStackTrace(System.out);
        }
      }
    }
      /**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread.
 * @throws UnsupportedLookAndFeelException 
 */
public static void createAndShowGUI() throws UnsupportedLookAndFeelException {

//  if(co == ComponentOrientation.LEFT_TO_RIGHT )
    UIManager.setLookAndFeel(new SubstanceLookAndFeel());
	
    //Create and set up the window.
   //if(co == ComponentOrientation.RIGHT_TO_LEFT )
	 //  JFrame.setDefaultLookAndFeelDecorated(true);
    frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gui = new ArMT();
    frame.setTitle(ArMT.labels.getString("programTitle"));
    int inset = 30;
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setBounds(inset, inset, screenSize.width  - inset*2,screenSize.height - inset*2); 
    frame.getContentPane().add(gui);
    frame.setJMenuBar(gui.createMenuBar());
    ArMT.applyComponentOrientation(gui.createToolBar(),co);
    frame.getContentPane().add(gui.createToolBar(),BorderLayout.PAGE_START);
    ArMT.applyComponentOrientation(frame,co);
    frame.setComponentOrientation(co);
    /************************************/
    final TrayIcon trayIcon;
    if (SystemTray.isSupported()) {

        SystemTray tray = SystemTray.getSystemTray();
        Image image = Toolkit.getDefaultToolkit().getImage("resources"+FILE_SEPARATOR+"images"+FILE_SEPARATOR+"_.png");

        MouseListener mouseListener = new MouseListener() {
                    
            public void mouseClicked(MouseEvent e) {              
            }

            public void mouseEntered(MouseEvent e) {
                
            }

            public void mouseExited(MouseEvent e) {                 
            }

            public void mousePressed(MouseEvent e) {                
            }

            public void mouseReleased(MouseEvent e) {                
            }
        };
        ActionListener exitListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        };        
        PopupMenu popup = new PopupMenu();
        MenuItem defaultItem = new MenuItem(ArMT.labels.getString("fileMenuExitItem"));
        defaultItem.addActionListener(exitListener);
        popup.add(defaultItem);
        trayIcon = new TrayIcon(image, ArMT.labels.getString("programTitle"), popup);
        ActionListener actionListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                trayIcon.displayMessage("Action Event", 
                    "An Action Event Has Been Performed!",
                    TrayIcon.MessageType.INFO);
            }
        };                
        trayIcon.setImageAutoSize(true);
        trayIcon.addActionListener(actionListener);
        trayIcon.addMouseListener(mouseListener);
        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
        }

    }
    frame.setVisible(true);
    
    
}
public static void appendToInput(String s)
{
	TranslationFrame.getInputTextArea().append(s);

}
/* */
// {
//   try {
//	createAndShowGUI();
//} catch (UnsupportedLookAndFeelException e) {
//	// TODO Auto-generated catch block
//	e.printStackTrace();
//}
//}

}
